#include <string.h>
#include <stdlib.h>

#include "IfList.h"

enum {
    NAME_COL,
    TYPE_COL,
    STATUS_COL,
    LINK_COL,
    ADDRS_COL,
    ROUTES_COL,
    NAME_WEIGHT_COL,
    N_COLS
};

void IfList_Init (IfList *o)
{
    LinkedList1_Init(&o->entries_list);
    
    o->store = gtk_list_store_new(N_COLS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT);
    
    o->widget = gtk_tree_view_new_with_model(GTK_TREE_MODEL(o->store));
    
    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Name", renderer, "text", NAME_COL, "weight", NAME_WEIGHT_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Type", renderer, "text", TYPE_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Status", renderer, "text", STATUS_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Link", renderer, "text", LINK_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Addresses", renderer, "text", ADDRS_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
    
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Routes", renderer, "text", ROUTES_COL, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(o->widget), column);
}

void IfList_Free (IfList *o)
{
    ASSERT(LinkedList1_IsEmpty(&o->entries_list))
}

GtkWidget * IfList_Widget (IfList *o)
{
    return o->widget;
}

void IfListEntry_Init (IfListEntry *o, IfList *iflist, const char *ifname)
{
    o->iflist = iflist;
    o->ifname = strdup(ifname);
    LinkedList1_Append(&iflist->entries_list, &o->entries_list_node);
    gtk_list_store_append(iflist->store, &o->iter);
}

void IfListEntry_Free (IfListEntry *o)
{
    IfList *iflist = o->iflist;
    
    gtk_list_store_remove(iflist->store, &o->iter);
    LinkedList1_Remove(&iflist->entries_list, &o->entries_list_node);
    free(o->ifname);
}

void IfListEntry_SetData (IfListEntry *o, const char *type, int link_is_up, const char *addresses, const char *routes, const char *status, int is_inet)
{
    IfList *iflist = o->iflist;
    
    const char *link_str = link_is_up ? "up" : "down";
    gint weight = is_inet ? 600 : 400;
    
    gtk_list_store_set(iflist->store, &o->iter, NAME_COL, o->ifname, TYPE_COL, type, LINK_COL, link_str, ADDRS_COL, addresses, ROUTES_COL, routes, STATUS_COL, status, NAME_WEIGHT_COL, weight, -1);
}
